package lib.XML;

import lib.Base.Education;
import lib.Base.Info;
import lib.Base.Message;
import lib.Base.Post;
import lib.Base.Profile;
import lib.Base.Workplace;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public class ProfileContentHandler implements ContentHandler {
	/**
	 * Class to handle XML content for profile's
	 */
	private String currentValue;
	private Profile profile;
	private Post post;
	private Info info;
	private Education education;
	private Workplace workplace;

	private long time;
	private String UserId;
	private String Text;
	private boolean GuestbookFlag = false;
	private boolean PostFlag = false;
	private boolean CommentsFlag = false;
	private boolean EducationFlag = false;
	private boolean WorkplaceFlag = false;

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		currentValue = new String(ch, start, length);
	}

	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		if (localName.equals("profile")) {
			profile = new Profile();
		} else if (localName.equals("message")) {
			time = Long.parseLong(atts.getValue("time"));
		} else if (localName.equals("post")) {
			PostFlag = true;
			post = new Post(Integer.parseInt(atts.getValue("id")));
		} else if (localName.equals("comments")) {
			CommentsFlag = true;
		} else if (localName.equals("guestbook")) {
			GuestbookFlag = true;
		} else if (localName.equals("info")) {
			info = new Info();
		} else if (localName.equals("education")) {
			education = new Education(Integer.parseInt(atts.getValue("id")));
			EducationFlag = true;
		} else if (localName.equals("workplace")) {
			workplace = new Workplace(Integer.parseInt(atts.getValue("id")));
			WorkplaceFlag = true;
		}
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (localName.equals("id")) {
			profile.setUserId(currentValue);
		} else if (localName.equals("picture")) {
			if (PostFlag) {
				post.setPictureId(currentValue);
			} else {
				profile.setPictureId(currentValue);
			}
		} else if (localName.equals("name")) {
			profile.setUsername(currentValue);
		} else if (localName.equals("lastChange")) {
			profile.setLastChange(Long.parseLong(currentValue));
		} else if (localName.equals("userId")) {
			if (GuestbookFlag || CommentsFlag) {
				UserId = currentValue;
			}
		} else if (localName.equals("text")) {
			if (GuestbookFlag || CommentsFlag) {
				Text = currentValue;
			} else if (PostFlag) {
				post.setText(currentValue);
			}
		} else if (localName.equals("time")) {
			if (PostFlag) {
				post.setTime(Long.parseLong(currentValue));
			}
		} else if (localName.equals("message")) {
			if (GuestbookFlag) {
				profile.addGuestbookMessage(new Message(time, UserId, Text));
			} else if (CommentsFlag) {
				post.addComment(new Message(time, UserId, Text));
			}
			time = 0;
			UserId = null;
			Text = null;
			GuestbookFlag = false;
		} else if (localName.equals("guestbook")) {
			profile.sortGuestbook();
			GuestbookFlag = false;
		} else if (localName.equals("comments")) {
			post.sortComments();
			CommentsFlag = false;
		} else if (localName.equals("post")) {
			profile.addPost(post);
			post = null;
		} else if (localName.equals("info")) {
			profile.setInfo(info);
		} else if (localName.equals("hobby")) {
			info.addHobby(currentValue);
		} else if (localName.equals("firstName")) {
			info.setFirstName(currentValue);
		} else if (localName.equals("lastName")) {
			info.setLastName(currentValue);
		} else if (localName.equals("about")) {
			info.setAbout(currentValue);
		} else if (localName.equals("education")) {
			info.addEducations(education);
			EducationFlag = false;
		} else if (localName.equals("workplace")) {
			info.addWorkplace(workplace);
			WorkplaceFlag = false;
		} else if (localName.equals("name")) {
			if (WorkplaceFlag) {
				workplace.setName(currentValue);
			} else if (EducationFlag) {
				education.setName(currentValue);
			}
		} else if (localName.equals("begin")) {
			if (WorkplaceFlag) {
				workplace.setBegin(Integer.parseInt(currentValue));
			} else if (EducationFlag) {
				education.setBegin(Integer.parseInt(currentValue));
			}
		} else if (localName.equals("end")) {
			if (WorkplaceFlag) {
				workplace.setEnd(Integer.parseInt(currentValue));
			} else if (EducationFlag) {
				education.setEnd(Integer.parseInt(currentValue));
			}
		} else if (localName.equals("profession")) {
			if (WorkplaceFlag) {
				workplace.setProfession(currentValue);
			}
		} else if (localName.equals("day")) {
			info.setGebDay(Integer.parseInt(currentValue));
		} else if (localName.equals("month")) {
			info.setGebMonth(Integer.parseInt(currentValue));
		} else if (localName.equals("year")) {
			info.setGebYear(Integer.parseInt(currentValue));
		}
	}

	public Profile getProfile() {
		return profile;
	}

	public void endDocument() throws SAXException {
	}

	public void endPrefixMapping(String prefix) throws SAXException {
	}

	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
	}

	public void processingInstruction(String target, String data)
			throws SAXException {
	}

	public void setDocumentLocator(Locator locator) {
	}

	public void skippedEntity(String name) throws SAXException {
	}

	public void startDocument() throws SAXException {
	}

	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
	}
}
